//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.payment;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.*;
import java.beans.PropertyVetoException;
import java.math.BigDecimal;
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.*;
import org.jpgolay.gui.date.DateComboBox;
import org.jpgolay.gui.fixed.*;
import org.jpgolay.me.reminder.Reminder;
import org.jpgolay.me.reminder.ReminderCRUD;
import org.jpgolay.me.util.*;

// Referenced classes of package org.jpgolay.me.payment:
//            PaymentCRUD, PaymentUpdatePiece, PaymentAdvanceSearchGUI, PopupPaymentGUI, 
//            Payment
public class PaymentController {

    private class MouseClickList extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            actionView();
        }

        MouseClickList() {
        }
    }

    public class LabelCellRenderer extends DefaultTableCellRenderer {

        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
            Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
            label = (JLabel) comp;
            label.setForeground(Color.WHITE);
            return label;
        }
        protected JLabel label;

        public LabelCellRenderer() {
        }
    }

    public PaymentController(SearchBox paymentSearch, TableViewer paymentListTable, TableViewer paymentTable, TableViewer reminderTable, Parameter param, JDesktopPane desktop) {
        this.paymentSearch = null;
        this.paymentListTable = null;
        this.paymentTable = null;
        this.reminderTable = null;
        paymentSearchGui = null;
        calendarPeriod = null;
        dateComboBox = null;
        updateRowsTable = true;
        write = false;
        this.paymentSearch = paymentSearch;
        this.paymentListTable = paymentListTable;
        this.paymentTable = paymentTable;
        this.reminderTable = reminderTable;
        this.param = param;
        this.desktop = desktop;
        date = new MEDate();
        crud = new PaymentCRUD(param);
        reminderCrud = new ReminderCRUD();
        labelRenderer = new LabelCellRenderer();
        updatePiece = new PaymentUpdatePiece(param);
        initLaw();
        initSearch();
        initListTable();
        initPaymentTable();
        initReminderTable();
        initPopupPaymentListener();
    }

    private void initLaw() {
        if (param.getAccessLaw().length() >= 29 && param.getAccessLaw().charAt(29) == '1') {
            write = true;
        }
    }

    private void initSearch() {
        paymentSearch.setList(new String[]{
                    "Code", "Raison social", "Numero", "Commercial (nom)", "Commercial (code)", "Par montant (TTC)"
                });
        paymentSearch.setVal("");
        paymentSearch.setAdvanceSearch(getPaymentSearchGUI());
        paymentSearch.getButtonRight().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionSearch();
            }
        });
        paymentSearch.getTextField().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionSearch();
            }
        });
    }

    private void initListTable() {
        paymentListTable.setTypes(new Class[]{
                    java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.math.BigDecimal.class, javax.swing.Icon.class
                });
        paymentListTable.setLastColumnNotEditable(1);
        paymentListTable.setCellEditable(false);
        paymentListTable.setAlternateColor(MEUtility.PIECES_LINE);
        paymentListTable.setColumnNames(new String[]{
                    "NUMERO", "DATE", "DESTINATAIRE", "COMMERCIAL", "TTC", "ETAT"
                });
        paymentListTable.setPreferredColumnSize(new int[]{
                    40, 40, 50, 50, 40, 20
                });
        actionSearch();
    }

    private void initPaymentTable() {
        paymentTable.setTypes(new Class[]{
                    java.lang.Integer.class, java.lang.String.class, java.math.BigDecimal.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
                });
        paymentTable.setLastColumnNotEditable(0);
        paymentTable.setCellEditable(false);
        paymentTable.setAlternateColor(MEUtility.PAYMENT_LINE);
        paymentTable.setPreferredColumnSize(new int[]{
                    1, 100, 100, 100, 100, 100
                });
        paymentTable.setColumnNames(new String[]{
                    "", "DATE", "MONTANT", "MODE", "NUMERO", "POINTAGE"
                });
        Object data[][] = new Object[10][6];
        for (int i = 0; i < data.length; i++) {
            data[i][0] = null;
            data[i][1] = null;
            data[i][2] = null;
            data[i][3] = null;
            data[i][4] = null;
            data[i][5] = null;
        }

        paymentTable.setData(data);
    }

    private void initReminderTable() {
        reminderTable.setTypes(new Class[]{
                    java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.Boolean.class
                });
        reminderTable.setLastColumnNotEditable(0);
        reminderTable.setCellEditable(false);
        reminderTable.setAlternateColor(MEUtility.REMINDER_LINE);
        reminderTable.setPreferredColumnSize(new int[]{
                    1, 200, 200, 10
                });
        reminderTable.setColumnNames(new String[]{
                    "", "DATE DE RELANCE", "NOTES", "?"
                });
        Object data[][] = new Object[10][4];
        for (int i = 0; i < data.length; i++) {
            data[i][0] = null;
            data[i][1] = null;
            data[i][2] = null;
            data[i][3] = new Boolean(false);
        }

        reminderTable.setData(data);
    }

    private PaymentAdvanceSearchGUI getPaymentSearchGUI() {
        if (paymentSearchGui == null) {
            paymentSearchGui = new PaymentAdvanceSearchGUI();
            paymentSearchGui.setAgency(0);
            paymentSearchGui.setPieceType(0);
            paymentSearchGui.getButtonRight().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    actionCalendarPeriod();
                }
            });
        }
        return paymentSearchGui;
    }

    private CalendarPeriod getCalendarPeriod() {
        if (calendarPeriod == null) {
            calendarPeriod = new CalendarPeriod();
            calendarPeriod.setSelectedPeriod(2);
            getPaymentSearchGUI().setPeriode(calendarPeriod.getMessage());
            calendarPeriod.getButtonLeft().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    actionCalendarPeriodOK();
                }
            });
            calendarPeriod.getButtonRight().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    actionCalendarPeriodCANCEL();
                }
            });
            calendarPeriod.setLocation(600, 50);
            calendarPeriod.setSize(320, 130);
        }
        return calendarPeriod;
    }

    private void setDateColumn(JTable table, TableColumn dateColumn) {
        DateComboBox comboBox = new DateComboBox();
        dateColumn.setCellEditor(new DefaultCellEditor(comboBox));
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setToolTipText("Cliquer pour s\351l\351ctionner la date du r\351glement");
        dateColumn.setCellRenderer(renderer);
    }

    private void setModeColumn(JTable table, TableColumn modeColumn) {
        JComboBox comboBox = new JComboBox();
        comboBox.addItem("Ch\351que");
        comboBox.addItem("Carte Bancaire");
        comboBox.addItem("Esp\350ces");
        comboBox.addItem("Avoir sur Facture");
        comboBox.addItem("Traite");
        comboBox.addItem("Pr\351l\350vement");
        comboBox.addItem("Autre");
        modeColumn.setCellEditor(new DefaultCellEditor(comboBox));
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setToolTipText("Cliquer pour s\351l\351ctionner un mode de r\351glement");
        modeColumn.setCellRenderer(renderer);
    }

    private void setCheckColumn(JTable table, TableColumn pointageColumn) {
        JComboBox comboBox = new JComboBox();
        comboBox.addItem("Encaiss\351");
        comboBox.addItem("En attente");
        comboBox.addItem("Mise en banque");
        comboBox.addItem("Refus\351");
        comboBox.addItem("Contentieux");
        comboBox.addItem("Injonction");
        comboBox.addItem("Huissier");
        comboBox.addItem("Perte");
        comboBox.addItem("Annul\351");
        comboBox.addItem("Pay\351");
        pointageColumn.setCellEditor(new DefaultCellEditor(comboBox));
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setToolTipText("Cliquer pour pointer le r\351glement");
        pointageColumn.setCellRenderer(renderer);
    }

    private PopupPaymentGUI getPopupPayment() {
        if (popupPayment == null) {
            popupPayment = new PopupPaymentGUI();
        }
        return popupPayment;
    }

    private void initPopupPaymentListener() {
        getPopupPayment().getDeleteLine().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionDeletePaymentLine();
            }
        });
    }

    private void actionSearch() {
        String from = getCalendarPeriod().getFromDate();
        String to = getCalendarPeriod().getToDate();
        int agency = getPaymentSearchGUI().getAgency() + 1;
        int piece = getPaymentSearchGUI().getPieceType();
        StringBuffer sql = new StringBuffer();
        if (paymentSearch.getSelectedIndex() == 1) {
            if (piece == 3 || piece == 7) {
                sql.append("SELECT achats.id, date, raison_sociale, achats.commerciaux_id, net_a_payer, montant_regle FROM achats, fournisseurs ");
            } else if (piece == 4 || piece == 5 || piece == 6) {
                sql.append("SELECT ventes.id, date, raison_sociale, ventes.commerciaux_id, net_a_payer, montant_regle FROM ventes, clients ");
            } else {
                sql.append("SELECT ventes_factures.id, date, raison_sociale, ventes_factures.commerciaux_id, net_a_payer, montant_regle FROM ventes_factures, clients ");
            }
        } else if (paymentSearch.getSelectedIndex() == 3) {
            if (piece == 3 || piece == 7) {
                sql.append("SELECT achats.id, date, fournisseurs_id, nom, net_a_payer, montant_regle FROM achats, commerciaux ");
            } else if (piece == 4 || piece == 5 || piece == 6) {
                sql.append("SELECT ventes.id, date, clients_id, nom, net_a_payer, montant_regle FROM ventes, commerciaux ");
            } else {
                sql.append("SELECT ventes_factures.id, date, clients_id, nom, net_a_payer, montant_regle FROM ventes_factures, commerciaux ");
            }
        } else if (piece == 3 || piece == 7) {
            sql.append("SELECT id, date, fournisseurs_id, commerciaux_id, net_a_payer, montant_regle FROM achats ");
        } else if (piece == 4 || piece == 5 || piece == 6) {
            sql.append("SELECT id, date, clients_id, commerciaux_id, net_a_payer, montant_regle FROM ventes ");
        } else {
            sql.append("SELECT id, date, clients_id, commerciaux_id, net_a_payer, montant_regle FROM ventes_factures ");
        }
        switch (piece) {
            case 0: // '\0'
                sql.append("WHERE prefixe = 'F'");
                prefix = "F-CL";
                break;

            case 1: // '\001'
                sql.append("WHERE prefixe = 'A'");
                prefix = "A-CL";
                break;

            case 2: // '\002'
                sql.append("WHERE prefixe = 'F' OR prefixe = 'A'");
                prefix = "FA-CL";
                break;

            case 3: // '\003'
                sql.append("WHERE prefixe = 'F'");
                prefix = "F-FR";
                break;

            case 4: // '\004'
                sql.append("WHERE prefixe = 'D'");
                prefix = "D-CL";
                break;

            case 5: // '\005'
                sql.append("WHERE prefixe = 'C'");
                prefix = "C-CL";
                break;

            case 6: // '\006'
                sql.append("WHERE prefixe = 'BL'");
                prefix = "BL-CL";
                break;

            case 7: // '\007'
                sql.append("WHERE prefixe = 'C'");
                prefix = "C-FR";
                break;
        }
        if (agency != 6) {
            sql.append(" AND entreprise_id=");
            sql.append(agency);
        }
        if (!getPaymentSearchGUI().isAllPieces()) {
            sql.append(" AND montant_regle != net_a_payer");
        }
        sql.append(" AND date BETWEEN '");
        sql.append(date.toMysql(from));
        sql.append("' AND '");
        sql.append(date.toMysql(to));
        switch (paymentSearch.getSelectedIndex()) {
            case 0: // '\0'
                if (piece == 3 || piece == 7) {
                    if (paymentSearch.getVal().equals("")) {
                        sql.append("' ORDER BY fournisseurs_id");
                    } else {
                        sql.append("' AND fournisseurs_id LIKE '");
                        sql.append(paymentSearch.getVal());
                        sql.append("%' ORDER BY fournisseurs_id");
                    }
                    break;
                }
                if (paymentSearch.getVal().equals("")) {
                    sql.append("' ORDER BY clients_id");
                } else {
                    sql.append("' AND clients_id LIKE '");
                    sql.append(paymentSearch.getVal());
                    sql.append("%' ORDER BY clients_id");
                }
                break;

            case 1: // '\001'
                if (piece == 3 || piece == 7) {
                    if (paymentSearch.getVal().equals("")) {
                        sql.append("' AND fournisseurs_id=fournisseurs.id");
                        sql.append(" ORDER BY raison_sociale");
                    } else {
                        sql.append("' AND fournisseurs_id=fournisseurs.id");
                        sql.append(" AND raison_sociale LIKE '");
                        sql.append(paymentSearch.getVal());
                        sql.append("%' ORDER BY raison_sociale");
                    }
                    break;
                }
                if (paymentSearch.getVal().equals("")) {
                    sql.append("' AND clients_id=clients.id");
                    sql.append(" ORDER BY raison_sociale");
                } else {
                    sql.append("' AND clients_id=clients.id");
                    sql.append(" AND raison_sociale LIKE '");
                    sql.append(paymentSearch.getVal());
                    sql.append("%' ORDER BY raison_sociale");
                }
                break;

            case 2: // '\002'
                if (paymentSearch.getVal().equals("")) {
                    sql.append("' ORDER BY id");
                } else {
                    sql.append("' AND id LIKE '");
                    sql.append(paymentSearch.getVal());
                    sql.append("%' ORDER BY id");
                }
                break;

            case 3: // '\003'
                if (paymentSearch.getVal().equals("")) {
                    sql.append("' AND commerciaux_id=commerciaux.id");
                    sql.append(" ORDER BY nom");
                } else {
                    sql.append("' AND commerciaux_id=commerciaux.id");
                    sql.append(" AND nom LIKE '");
                    sql.append(paymentSearch.getVal());
                    sql.append("%' ORDER BY nom");
                }
                break;

            case 4: // '\004'
                if (paymentSearch.getVal().equals("")) {
                    sql.append("' ORDER BY commerciaux_id");
                } else {
                    sql.append("' AND commerciaux_id LIKE '");
                    sql.append(paymentSearch.getVal());
                    sql.append("%' ORDER BY commerciaux_id");
                }
                break;

            case 5: // '\005'
                if (paymentSearch.getVal().equals("")) {
                    sql.append("' ORDER BY net_a_payer");
                } else {
                    sql.append("' AND net_a_payer LIKE '");
                    sql.append(paymentSearch.getVal());
                    sql.append("%' ORDER BY net_a_payer");
                }
                break;

            default:
                if (piece == 3 || piece == 7) {
                    if (paymentSearch.getVal().equals("")) {
                        sql.append("' ORDER BY fournisseurs_id");
                    } else {
                        sql.append("' AND fournisseurs_id LIKE '");
                        sql.append(paymentSearch.getVal());
                        sql.append("%' ORDER BY fournisseurs_id");
                    }
                    break;
                }
                if (paymentSearch.getVal().equals("")) {
                    sql.append("' ORDER BY clients_id");
                } else {
                    sql.append("' AND clients_id LIKE '");
                    sql.append(paymentSearch.getVal());
                    sql.append("%' ORDER BY clients_id");
                }
                break;
        }
        Object data[][] = crud.getDataList(sql.toString(), prefix);
        if (data != null) {
            paymentListTable.setData(data);
            paymentListTable.getTable().addMouseListener(new MouseClickList());
        } else {
            paymentListTable.setData(getEmptyTable());
        }
    }

    private Object[][] getEmptyTable() {
        Object data[][] = new Object[3][6];
        for (int i = 0; i < data.length; i++) {
            data[i][0] = null;
            if (i == 1) {
                data[i][1] = "AUCUN";
                data[i][2] = "REGLEMENT";
            } else {
                data[i][1] = null;
                data[i][2] = null;
            }
            data[i][3] = null;
            data[i][4] = null;
            data[i][5] = null;
        }

        return data;
    }

    private void actionCalendarPeriod() {
        getCalendarPeriod().setVisible(true);
        getCalendarPeriod().toFront();
        desktop.add(getCalendarPeriod(), JLayeredPane.DEFAULT_LAYER);
        try {
            getCalendarPeriod().setSelected(true);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
    }

    private void actionCalendarPeriodOK() {
        getPaymentSearchGUI().setPeriode(getCalendarPeriod().getMessage());
        actionCalendarPeriodCANCEL();
    }

    private void actionCalendarPeriodCANCEL() {
        getCalendarPeriod().setVisible(false);
        desktop.remove(getCalendarPeriod());
    }

    private void actionView() {
        if (!paymentTable.isCellEditable()) {
            paymentTable.setCellEditable(write);
        }
        pieceId = (String) paymentListTable.getTable().getValueAt(paymentListTable.getTable().getSelectedRow(), 0);
        piecePrice = (BigDecimal) paymentListTable.getTable().getValueAt(paymentListTable.getTable().getSelectedRow(), 4);
        switch (getPaymentSearchGUI().getPieceType()) {
            case 0: // '\0'
                pieceType = 3;
                break;

            case 1: // '\001'
                pieceType = 4;
                break;

            case 2: // '\002'
                pieceType = 3;
                break;

            case 3: // '\003'
                pieceType = 6;
                break;

            case 4: // '\004'
                pieceType = 0;
                break;

            case 5: // '\005'
                pieceType = 1;
                break;

            case 6: // '\006'
                pieceType = 2;
                break;

            case 7: // '\007'
                pieceType = 5;
                break;
        }
        Object data[][] = crud.getDataPayment(pieceId, prefix);
        paymentTable.setData(data);
        TableColumnModel model = paymentTable.getTable().getColumnModel();
        model.getColumn(0).setCellRenderer(labelRenderer);
        setDateColumn(paymentTable.getTable(), paymentTable.getTable().getColumnModel().getColumn(1));
        setModeColumn(paymentTable.getTable(), paymentTable.getTable().getColumnModel().getColumn(3));
        setCheckColumn(paymentTable.getTable(), paymentTable.getTable().getColumnModel().getColumn(5));
        paymentTable.getTable().addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() != 1 && paymentTable.isCellEditable()) {
                    getPopupPayment().show(e.getComponent(), e.getX(), e.getY());
                }
            }
        });
        paymentTable.getTableModel().addTableModelListener(new TableModelListener() {
            public void tableChanged(TableModelEvent e) {
                if (updateRowsTable) {
                    actionUpdatePaymentRow(e.getFirstRow());
                }
            }
        });
        if (getPaymentSearchGUI().getPieceType() <= 3) {
            if (!reminderTable.isCellEditable()) {
                reminderTable.setCellEditable(write);
            }
            data = reminderCrud.getDataReminder(pieceId);
            reminderTable.setData(data);
            model = reminderTable.getTable().getColumnModel();
            model.getColumn(0).setCellRenderer(labelRenderer);
            setDateColumn(reminderTable.getTable(), reminderTable.getTable().getColumnModel().getColumn(1));
            reminderTable.getTableModel().addTableModelListener(new TableModelListener() {
                public void tableChanged(TableModelEvent e) {
                    actionUpdateReminderRow(e.getFirstRow());
                }
            });
        } else if (reminderTable.isCellEditable()) {
            reminderTable.setCellEditable(false);
        }
    }

    private void actionUpdateReminderRow(int row) {
        Reminder reminder = new Reminder();
        reminder.setId((Integer) reminderTable.getTable().getValueAt(row, 0));
        reminder.setDate((String) reminderTable.getTable().getValueAt(row, 1));
        reminder.setNotes((String) reminderTable.getTable().getValueAt(row, 2));
        Boolean b = (Boolean) reminderTable.getTable().getValueAt(row, 3);
        if (b.booleanValue()) {
            reminder.setStatus(new Integer(1));
        } else {
            reminder.setStatus(new Integer(0));
        }
        reminder.setUserId(param.getUserId());
        reminder.setPieceId(pieceId);
        if (reminder.getId() == null) {
            Integer pk = reminderCrud.insertReminderRow(reminder);
            reminderTable.getTable().setValueAt(pk, row, 0);
        } else if (!reminderCrud.updateReminderRow(reminder)) {
            JOptionPane.showMessageDialog(null, "Impossible d'enregistrer les donn\351es !");
        }
    }

    private void actionUpdatePaymentRow(int row) {
        Payment payment = new Payment();
        payment.setId((Integer) paymentTable.getTable().getValueAt(row, 0));
        payment.setDate((String) paymentTable.getTable().getValueAt(row, 1));
        payment.setMontant((BigDecimal) paymentTable.getTable().getValueAt(row, 2));
        payment.setMode((String) paymentTable.getTable().getValueAt(row, 3));
        payment.setNumero((String) paymentTable.getTable().getValueAt(row, 4));
        payment.setPointage((String) paymentTable.getTable().getValueAt(row, 5));
        payment.setUserId(param.getUserId());
        payment.setPieceId(pieceId);
        payment.setPrefix(prefix);
        if (payment.getId() == null) {
            if (payment.getDate() != null) {
                Integer pk = crud.insertPaymentRow(payment);
                paymentTable.getTable().setValueAt(pk, row, 0);
            }
        } else if (!crud.updatePaymentRow(payment, row)) {
            JOptionPane.showMessageDialog(null, "Impossible d'enregistrer les donn\351es !");
        }
        updatePiece(payment);
        actionSearch();
    }

    private void updatePiece(Payment payment) {
        int updateType = calculatePayment();
        if (updateType == 1) {
            JOptionPane.showMessageDialog(null, "R\351glement refus\351 !\nLa somme pay\351e d\351passe le montant \340 regler.");
        } else {
            updatePiece.add(pieceType, updateType, cash, pieceId);
        }
    }

    private int calculatePayment() {
        Object data[][] = paymentTable.getData();
        cash = new BigDecimal("0.00");
        for (int i = 0; i < data.length; i++) {
            if (data[i][0] != null && !data[i][1].equals(new String("")) && data[i][2] != null && data[i][5] != null && (data[i][5].equals(new String("Encaiss\351")) || data[i][5].equals(new String("Pay\351")))) {
                cash = cash.add((BigDecimal) data[i][2]);
            }
        }

        return cash.compareTo(piecePrice);
    }

    private void actionDeletePaymentLine() {
        int row = paymentTable.getTable().getSelectedRow();
        if (paymentTable.getTable().getValueAt(row, 0) != null && !paymentTable.getTable().getValueAt(row, 5).equals("Encaiss\351") && !paymentTable.getTable().getValueAt(row, 5).equals("Pay\351")) {
            Payment oldPayment = crud.getOldPayment(row);
            if (oldPayment.getPointage().equals("Encaiss\351") || oldPayment.getPointage().equals("Pay\351")) {
                updatePiece.remove(pieceType, (BigDecimal) paymentTable.getTable().getValueAt(row, 2), pieceId, piecePrice);
            }
            crud.deletePaymentRow((Integer) paymentTable.getTable().getValueAt(row, 0), row);
            updateRowsTable = false;
            paymentTable.getTable().setValueAt(null, row, 0);
            paymentTable.getTable().setValueAt(null, row, 1);
            paymentTable.getTable().setValueAt(null, row, 2);
            paymentTable.getTable().setValueAt(null, row, 3);
            paymentTable.getTable().setValueAt(null, row, 4);
            paymentTable.getTable().setValueAt(null, row, 5);
            updateRowsTable = true;
        }
    }

    public int getPieceType() {
        return pieceType;
    }
    private SearchBox paymentSearch;
    private TableViewer paymentListTable;
    private TableViewer paymentTable;
    private TableViewer reminderTable;
    private PaymentAdvanceSearchGUI paymentSearchGui;
    private CalendarPeriod calendarPeriod;
    private PaymentCRUD crud;
    private JDesktopPane desktop;
    private Parameter param;
    private LabelCellRenderer labelRenderer;
    private String prefix;
    private DateComboBox dateComboBox;
    private MEDate date;
    private String pieceId;
    private BigDecimal piecePrice;
    private BigDecimal cash;
    private PaymentUpdatePiece updatePiece;
    private int pieceType;
    private ReminderCRUD reminderCrud;
    private PopupPaymentGUI popupPayment;
    private boolean updateRowsTable;
    private boolean write;
}
